package com.stedu.service.impl;

import com.stedu.bean.Repair;
import com.stedu.exception.SteduException;
import com.stedu.mapper.RepairMapper;
import com.stedu.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class RepairServiceImpl implements RepairService {
    @Autowired
    private RepairMapper repairMapper;
    @Override
    public List<Repair> selectByOname(String oname) throws SteduException {
        return repairMapper.selectByOname(oname);
    }
    @Override
    public List<Repair> selectByOid(Integer oid)  {
        return repairMapper.selectByOid(oid);
    }
    @Override
    public List<Repair> selectByPid(Integer pid)  {
        return repairMapper.selectByPid(pid);
    }

    @Override
    public boolean insert(Repair e) throws SteduException {
        if(repairMapper.selectByRid(e.getRid()) !=null){
            throw new SteduException("该报修任务已经存在，无法重复添加");
        }
        return repairMapper.insert(e) == 1;
    }

    @Override
    public boolean delete(Integer rid) throws SteduException {
        if (repairMapper.selectByRid(rid) ==null){
            throw new SteduException("该报修任务不存在，无法删除");

        }
        return repairMapper.delete(rid)==1;
    }

    @Override
    public boolean update(Repair r) throws SteduException {
        if (repairMapper.selectByRid(r.getRid()) == null){
            throw new SteduException("该报修任务不存在，无法修改");
        }
        return repairMapper.update(r) == 1;
    }

    @Override
    public Repair selectByRid(Integer rid) {
        return repairMapper.selectByRid(rid);
    }

    @Override
    public List<Repair> selectAll() {
        return repairMapper.selectAll();
    }

    @Override
    public List<Integer> selectPidByRid(Integer rid) {
        return repairMapper.selectPidByRid(rid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertRidAndPid(Integer rid, Integer[] pids) {
        //删除某个维修项目与维修人员的关联关系
        repairMapper.deleteRidAndPidByRid(rid);
        //添加某个维修项目与维修人员的关联关系
        if(pids.length != 0) {
            repairMapper.insertRidAndPid(rid, pids);
        }
    }

}
